package info.demo.service;

import info.demo.entity.EntidadePersistente;
import info.demo.persistence.GenericDAO;

import java.io.Serializable;
import java.util.List;


public abstract class AbstractService<G extends GenericDAO<T, ID>, T extends EntidadePersistente, ID extends Serializable>
		implements Serializable, GenericService<T, ID> {

    private static final long serialVersionUID = -5569784615960930635L;

	protected G serviceDAO;

	public void setServiceDAO(G serviceDAO) {
		this.serviceDAO = serviceDAO;
	}

	@SuppressWarnings("unchecked")
	public T getById(ID id, boolean lock) throws ServiceException {
		return this.serviceDAO.findById(id, lock);
	}

	public T getById(ID id) throws ServiceException {
		return this.serviceDAO.findById(id);
	}

	@SuppressWarnings("unchecked")
	public List<T> findAll() throws ServiceException {
		return this.serviceDAO.findAll();
	}

	@SuppressWarnings("unchecked")
	public T saveOrUpdate(T entity) throws ServiceException {
		return this.serviceDAO.saveOrUpdate(entity);
	}

	public T save(T entity) throws ServiceException {
		return this.serviceDAO.save(entity);
	}

	public T update(T entity) throws ServiceException {
		return this.serviceDAO.update(entity);
	}

	public T delete(T entity) throws ServiceException {
		return this.serviceDAO.delete(entity);
	}


    public void deleteById(ID id) throws ServiceException {
        this.serviceDAO.delete(this.serviceDAO.getById(id));
    }

}
